#include "pixel_processing.h"



void rgb565_to_rgb888_pixel(uint16_t* pixel_565, void* pixel_888)
{
    uint8_t* r = (uint8_t*)pixel_888;
    uint8_t* g = r + 1;
    uint8_t* b = g + 1;
    // 提取各颜色分量
    *r = (uint8_t)((*pixel_565 >> 11) & 0x1F); // 取出红分量5位
    *g = (uint8_t)((*pixel_565 >> 5) & 0x3F); // 取出绿分量6位
    *b = (uint8_t)((*pixel_565 >> 0) & 0x1F); // 取出蓝分量5位

    // 扩展到8位：将低精度颜色值映射到0~255范围
    *r = (*r << 3) | (*r >> 2); // 左移3位后补低位 255/31 = 8.2258... 约等于 2^3 + 2^(-2)
    *g = (*g << 2) | (*g >> 4); // 6位 -> 8位 255/63 = 4.0634... 约等于 2^2 + 2^(-4)
    *b = (*b << 3) | (*b >> 2); // 同红色
}

void rgb565_to_rgb888_array(uint16_t* array_565, void* array_888, size_t count)
{
    uint16_t* Final_565 = array_565 + count - 1;
    
    uint16_t* it_565 = array_565;
    uint8_t* it_888 = (uint8_t*)array_888;
    while (it_565 <= Final_565)
    {
        rgb565_to_rgb888_pixel(it_565, it_888);
        it_565++;
        it_888 += 3;
    }
}